'Imports SQLDMO

Imports Microsoft.SqlServer.Server
Imports Microsoft.SqlServer.Management
Imports Microsoft.SqlServer.Management.Smo
Imports Microsoft.SqlServer.Management.Nmo
Imports System.Data.SqlClient
Imports System.Data.Sql
Imports System.IO
Imports System.Data
Imports System.Xml
Imports ICSharpCode
Imports ICSharpCode.SharpZipLib
Imports ICSharpCode.SharpZipLib.Zip
Public Class BackupManager
    Dim cData As Data
    Public sqlServer, UserName, Password, databasename, SQLServerDataDirectory, SQLServerBackupDirectory As String
    Function GetConfigData() As Boolean
        Dim XmlDoc As New XmlDocument()
        XmlDoc.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile)
        For Each xElement As XmlElement In XmlDoc.DocumentElement
            If xElement.Name = "appSettings" Then
                For Each xNode As XmlNode In xElement.ChildNodes
                    Select Case xNode.Attributes(0).Value
                        Case "Database"
                            databasename = xNode.Attributes(1).Value
                        Case "DatabaseUser"
                            UserName = xNode.Attributes(1).Value
                        Case "DatabasePassword"
                            Password = xNode.Attributes(1).Value
                        Case "DatabaseServer"
                            sqlServer = xNode.Attributes(1).Value
                        Case "SQLServerBackupDirectory"
                            SQLServerBackupDirectory = xNode.Attributes(1).Value
                        Case "SQLServerDataDirectory"
                            SQLServerDataDirectory = xNode.Attributes(1).Value
                    End Select
                Next
            End If
        Next
        Return True
    End Function
    'Function BackupDB(ByVal backupname As String, ByVal backupdate As Date) As Boolean
    '    Try
    '        GetConfigData()
    '        ''sqlServer, UserName, Password, databasename
    '        Dim backup As SQLDMO.Backup = New SQLDMO.Backup
    '        Dim sqlservercn As SQLDMO.SQLServer = New SQLDMO.SQLServer
    '        sqlservercn.LoginSecure = False
    '        sqlservercn.Connect(sqlServer, UserName, Password)
    '        backup.Action = SQLDMO.SQLDMO_BACKUP_TYPE.SQLDMOBackup_Database
    '        backup.Database = databasename
    '        backup.Files = backupname
    '        ''backup.BackupSetName = "DIGI_DOC_Full"
    '        backup.BackupSetDescription = "This Backup is from " & backupdate
    '        backup.Initialize = True
    '        backup.SQLBackup(sqlservercn)
    '        sqlservercn.DisConnect()
    '        Return True
    '    Catch ex As Exception
    '        MsgBox(ex.ToString)
    '        MessageBox.Show("Error Ocurre Trying to Backup Database" & vbCrLf & _
    '                         "If This Error Continue. Call For Assistant.", "Virtual Desk", _
    '                         MessageBoxButtons.OK, MessageBoxIcon.Error)
    '        Err.Clear()

    '        Return False
    '    End Try
    'End Function
    'Function RestoreDB(ByVal RestoreBackFile As String) As Boolean
    '    Try
    '        GetConfigData()
    '        ''sqlServer, UserName, Password, databasename
    '        Dim sqlservercn As SQLDMO.SQLServer = New SQLDMO.SQLServer
    '        Dim restore As SQLDMO.Restore = New SQLDMO.Restore
    '        sqlservercn.LoginSecure = False
    '        sqlservercn.Connect(sqlServer, UserName, Password)
    '        '' KillSQLProcesses(sqlservercn, "wascar", sqlServer, UserName, Password)
    '        restore.Action = SQLDMO.SQLDMO_RESTORE_TYPE.SQLDMORestore_Database
    '        restore.Database = databasename
    '        restore.Files = RestoreBackFile
    '        restore.FileNumber = 1

    '        'restore.LastRestore = False

    '        'restore.ReplaceDatabase = True
    '        restore.SQLRestore(sqlservercn)
    '        'restore.Action = SQLDMO_RESTORE_TYPE.SQLDMORestore_Log
    '        'restore.FileNumber = 2
    '        'restore.LastRestore = False
    '        'restore.SQLRestore(sqlservercn)
    '        sqlservercn.DisConnect()
    '    Catch ex As Exception
    '        MsgBox(ex.ToString)
    '        MessageBox.Show("Error Ocurre Trying to Restore Backup Database" & vbCrLf & _
    '                         "If This Error Continue. Call For Assistant.", "Virtual Desk", _
    '                         MessageBoxButtons.OK, MessageBoxIcon.Error)
    '        Err.Clear()

    '        Return False
    '    End Try
    'End Function
    'Function KillConnections() As Boolean
    '    GetConfigData()
    '    Dim oSQLServer As New SQLDMO.SQLServer
    '    Dim oDatabase As SQLDMO.Database
    '    Dim oSQL As New SQLDMO.Application
    '    oSQLServer.Connect(sqlServer, UserName, Password)
    '    oDatabase = oSQLServer.Databases.ItemByID(databasename)

    'End Function

    'Function DetachDb() As Boolean
    '    Dim sServer As New SQLDMO.SQLServer
    '    GetConfigData()
    '    sServer.Connect(sqlServer, UserName, Password)
    '    sServer.DetachDB(databasename, True)
    '    sServer.DisConnect()
    'End Function
    'Function AttachDb() As Boolean
    '    Dim sServer As New SQLDMO.SQLServer
    '    GetConfigData()
    '    Dim dbfiles As String = SQLServerDataDirectory & databasename & ".mdf"

    '    sServer.Connect(sqlServer, UserName, Password)
    '    sServer.AttachDBWithSingleFile(databasename, dbfiles)
    '    sServer.DisConnect()
    'End Function
    Function CreateZipFiles(ByVal OUTPATH As String, ByVal filename As String) As Boolean
        Dim outname As String = filename
        Try

            ''Me.DetachDb()
            Dim FILEOUTPUT As String = Me.SQLServerDataDirectory & databasename & ".mdf"
            Dim targetName As String = OUTPATH & outname & ".zip"
            If targetName.Length = 0 Then
                MessageBox.Show("No name specified", "Zip file name error")
                Return False
                Exit Function
            End If
            Dim strmZipOutputStream As ZipOutputStream
            strmZipOutputStream = New ZipOutputStream(File.Create(targetName))
            strmZipOutputStream.SetLevel(9)
            Dim strFile As String = FILEOUTPUT
            Dim strmFile As FileStream = File.OpenRead(FILEOUTPUT)
            Dim abyBuffer(strmFile.Length - 1) As Byte
            strmFile.Read(abyBuffer, 0, abyBuffer.Length)
            Dim objZipEntry As ZipEntry = New ZipEntry(strFile)
            objZipEntry.DateTime = DateTime.Now
            objZipEntry.Size = strmFile.Length
            strmFile.Close()
            strmZipOutputStream.PutNextEntry(objZipEntry)
            strmZipOutputStream.Write(abyBuffer, 0, abyBuffer.Length)
            strmZipOutputStream.Finish()
            strmZipOutputStream.Close()
            ''Me.AttachDb()
        Catch ex As Exception
            MessageBox.Show("One Error Occure when try do backup database." & vbCrLf & "Please try again. If error persist contact for support", "Virtual Desk", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Err.Clear()
            '' Me.AttachDb()
        End Try
    End Function
End Class